<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
       <el-row :gutter="24">
    <el-col :span="12" class="mb20">
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="form.deviceName" placeholder="请输入设备名称"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="异常码" prop="errorCode">
        <el-input v-model="form.errorCode" placeholder="请输入异常码"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="楼层编号" prop="floorNumber">
        <el-input-number :min="1" :max="1000" v-model="form.floorNumber" placeholder="请输入楼层编号"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="楼层名称" prop="floorName">
        <el-input v-model="form.floorName" placeholder="请输入楼层名称"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="运行状态" prop="runningStatus">
        <el-input-number :min="1" :max="1000" v-model="form.runningStatus" placeholder="请输入运行状态"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="平层状态" prop="levelingStatus">
        <el-input-number :min="1" :max="1000" v-model="form.levelingStatus" placeholder="请输入平层状态"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="门状态" prop="doorStatus">
        <el-input-number :min="1" :max="1000" v-model="form.doorStatus" placeholder="请输入门状态"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="运行速度" prop="speed">
        <el-input-number :min="1" :max="1000" v-model="form.speed" placeholder="请输入运行速度"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="气压" prop="press">
        <el-input-number :min="1" :max="1000" v-model="form.press" placeholder="请输入气压"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="电瓶车状态" prop="motoStatus">
        <el-input-number :min="1" :max="1000" v-model="form.motoStatus" placeholder="请输入电瓶车状态"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="人数" prop="personCount">
        <el-input-number :min="1" :max="1000" v-model="form.personCount" placeholder="请输入人数"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="温度" prop="tempe">
        <el-input-number :min="1" :max="1000" v-model="form.tempe" placeholder="请输入温度"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="湿度" prop="humid">
        <el-input-number :min="1" :max="1000" v-model="form.humid" placeholder="请输入湿度"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="二氧化碳浓度" prop="co2">
        <el-input-number :min="1" :max="1000" v-model="form.co2" placeholder="请输入二氧化碳浓度"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="烟雾状态" prop="smokeStatus">
        <el-input-number :min="1" :max="1000" v-model="form.smokeStatus" placeholder="请输入烟雾状态"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="火焰状态" prop="flameStatus">
        <el-input-number :min="1" :max="1000" v-model="form.flameStatus" placeholder="请输入火焰状态"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="累计运行次数" prop="runningCount">
        <el-input-number :min="1" :max="1000" v-model="form.runningCount" placeholder="请输入累计运行次数"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="累计开门次数" prop="runningOpenCount">
        <el-input-number :min="1" :max="1000" v-model="form.runningOpenCount" placeholder="请输入累计开门次数"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="累计里程" prop="runningMile">
        <el-input-number :min="1" :max="1000" v-model="form.runningMile" placeholder="请输入累计里程"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="累计运行时间" prop="runningTime">
        <el-input-number :min="1" :max="1000" v-model="form.runningTime" placeholder="请输入累计运行时间"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="总累计运行次数" prop="totalRunningCount">
        <el-input-number :min="1" :max="1000" v-model="form.totalRunningCount" placeholder="请输入总累计运行次数"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="总累计开门次数" prop="totalRunningOpenCount">
        <el-input-number :min="1" :max="1000" v-model="form.totalRunningOpenCount" placeholder="请输入总累计开门次数"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="总累计里程" prop="totalRunningMile">
        <el-input-number :min="1" :max="1000" v-model="form.totalRunningMile" placeholder="请输入总累计里程"></el-input-number>
      </el-form-item>
    </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="总累计运行时间" prop="totalRunningTime">
        <el-input-number :min="1" :max="1000" v-model="form.totalRunningTime" placeholder="请输入总累计运行时间"></el-input-number>
      </el-form-item>
    </el-col>

			</el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="DeviceMetaDataDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/evaboxcloud/deviceMetaData'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典
const { evabox_flame_status,evabox_door_status,evabox_leveling_status,evabox_moto_status,evabox_running_status,evabox_smoke_status } = useDict('evabox_flame_status','evabox_door_status','evabox_leveling_status','evabox_moto_status','evabox_running_status','evabox_smoke_status')

// 提交表单数据
const form = reactive({
		id:'',
	  deviceName: '',
	  errorCode: '',
		floorNumber: 0,
	  floorName: '',
		runningStatus: 0,
		levelingStatus: 0,
		doorStatus: 0,
		speed: 0,
		press: 0,
		motoStatus: 0,
		personCount: 0,
		tempe: 0,
		humid: 0,
		co2: 0,
		smokeStatus: 0,
		flameStatus: 0,
		runningCount: 0,
		runningOpenCount: 0,
		runningMile: 0,
		runningTime: 0,
		totalRunningCount: 0,
		totalRunningOpenCount: 0,
		totalRunningMile: 0,
		totalRunningTime: 0,
});

// 定义校验规则
const dataRules = ref({
        deviceName: [{required: true, message: '设备名称不能为空', trigger: 'blur'}],
        errorCode: [{required: true, message: '异常码不能为空', trigger: 'blur'}],
    floorNumber: [{required: true, message: '楼层编号不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
        floorName: [{required: true, message: '楼层名称不能为空', trigger: 'blur'}],
    runningStatus: [{required: true, message: '运行状态不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    levelingStatus: [{required: true, message: '平层状态不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    doorStatus: [{required: true, message: '门状态不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    speed: [{required: true, message: '运行速度不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    press: [{required: true, message: '气压不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    motoStatus: [{required: true, message: '电瓶车状态不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    personCount: [{required: true, message: '人数不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    tempe: [{required: true, message: '温度不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    humid: [{required: true, message: '湿度不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    co2: [{required: true, message: '二氧化碳浓度不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    smokeStatus: [{required: true, message: '烟雾状态不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    flameStatus: [{required: true, message: '火焰状态不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    runningCount: [{required: true, message: '累计运行次数不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    runningOpenCount: [{required: true, message: '累计开门次数不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    runningMile: [{required: true, message: '累计里程不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    runningTime: [{required: true, message: '累计运行时间不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    totalRunningCount: [{required: true, message: '总累计运行次数不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    totalRunningOpenCount: [{required: true, message: '总累计开门次数不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    totalRunningMile: [{required: true, message: '总累计里程不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
    totalRunningTime: [{required: true, message: '总累计运行时间不能为空', trigger: 'blur'}, { validator: rule.number, trigger: 'blur' }],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取deviceMetaData信息
  if (id) {
    form.id = id
    getdeviceMetaDataData(id)
  }
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
    loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getdeviceMetaDataData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>